package io.renren.modules.job.task;


import io.renren.common.utils.DateUtil;
import io.renren.common.utils.MapUtils;
import io.renren.common.utils.SmsUtil;
import io.renren.modules.project.service.ProjectManageService;
import io.renren.modules.project.service.RenProjectService;
import io.renren.modules.project.vo.ProjectVoEntity;
import io.renren.modules.ren.entity.RenRecordEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 定时任务
 * workTask为spring bean的名称
 * @author ygg
 * @since 2021-03-08
 */
@Component("workTask")
public class WorkTask {

    @Autowired
    private ProjectManageService projectManageService;

    @Autowired
    private SysUserService sysUserService;

    private Logger logger = LoggerFactory.getLogger(getClass());

    public void run(String params) {
        Map<String,Object> dateMap = new MapUtils().put("startDate",DateUtil.getPreMonthFirstDay()).put("endDate",DateUtil.getCurMonthLastDay());
        List<ProjectVoEntity> list = projectManageService.get90PList(dateMap);
        // 提示进度90%却超过一天没有提交作业
        Map<String,Object> proMap = new MapUtils();
        // 作业超时提醒，没有提交作业且超过最短工期
        Map<String,Object> workMap = new MapUtils();

        for (ProjectVoEntity voEntity : list) {
            if (voEntity.getScheduleTime() == null) {
                continue;
            }
            String beginDateStr = DateUtil.getFormatDay(voEntity.getScheduleTime());
            String endDateStr = DateUtil.getFormatDay(new Date());
            // 进度为90 ，工作完成时间为空 且 超时一天
            if (voEntity.getScheduleRate() == 90 && voEntity.getWFinishDateTime() == null
                    && DateUtil.getDaySub(beginDateStr,endDateStr) >= 1 && StringUtils.isNotBlank(voEntity.getProjectChargeAccount())) {
                List<ProjectVoEntity> newList;
                if (proMap.containsKey(voEntity.getProjectChargeAccount())) {
                    newList = ((List<ProjectVoEntity>)proMap.get(voEntity.getProjectChargeAccount()));
                } else {
                    newList = new ArrayList<ProjectVoEntity>();
                }
                newList.add(voEntity);
                proMap.put( voEntity.getProjectChargeAccount(), newList);
            }
            // 作业超时提醒
            beginDateStr = DateUtil.getFormatDay(voEntity.getProjectBegunDateTime());
            if (voEntity.getWFinishDateTime() == null && DateUtil.getDaySub(beginDateStr,endDateStr) >= voEntity.getProjectWorkDate()) {
                List<ProjectVoEntity> wList;

                if (workMap.containsKey(voEntity.getProjectChargeAccount())) {
                    wList = ((List<ProjectVoEntity>)workMap.get(voEntity.getProjectChargeAccount()));
                } else {
                    wList = new ArrayList<ProjectVoEntity>();
                }
                wList.add(voEntity);
                workMap.put( voEntity.getProjectChargeAccount(), wList);
            }

        }
        // 发送短信
        for(String key : proMap.keySet()){
            SysUserEntity userEntity = sysUserService.queryByUserAccount(key);
            if (userEntity != null && StringUtils.isNotBlank(userEntity.getPhonenumber())){
                continue;
            } else {
                // 发短信通知
                //项目名 个数
                String pName = ((List<ProjectVoEntity>)proMap.get(key)).get(0).getProjectName();
                String pCharge = ((List<ProjectVoEntity>)proMap.get(key)).get(0).getProjectCharge();
                int pNum = ((List<ProjectVoEntity>)proMap.get(key)).size();
                SmsUtil.sendWork90Msg(userEntity.getPhonenumber(), new String[]{pName,String.valueOf(pNum),pCharge});
            }
        }
        for(String key : workMap.keySet()){
            SysUserEntity userEntity = sysUserService.queryByUserAccount(key);
            if (userEntity != null && StringUtils.isNotBlank(userEntity.getPhonenumber())){
                continue;
            } else {
                // 发短信通知
                //项目名 个数
                String pName = ((List<ProjectVoEntity>)workMap.get(key)).get(0).getProjectName();
                String pCharge = ((List<ProjectVoEntity>)workMap.get(key)).get(0).getProjectCharge();
                int pNum = ((List<ProjectVoEntity>)workMap.get(key)).size();
                SmsUtil.sendWorkTimeOutMsg(userEntity.getPhonenumber(), new String[]{pName,String.valueOf(pNum),pCharge});
            }
        }
    }
}
